package com.degon.hsyl.anchorscrollview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class ScrollViewTheAnchor implements TabLayout.OnTabSelectedListener, ScrollViewListener, View.OnTouchListener {

    /**
     * 内部的tab
     */
    private TabLayout mInside_tab;

    /**
     * 外部的tab
     */
    private TabLayout mOutside_tab;

    private ObservableScrollView mScrollView;

    /**
     * 锚点集合
     */
    private ArrayList<TextView> mAimingPointList;

    private Context mContext;

    /**
     * scrollView的滑动状态
     */
    private int state = MotionEvent.ACTION_UP;

    /**
     * 覆盖的高度
     */
    private float mCoverHeight;

    @SuppressLint("ClickableViewAccessibility")
    public ScrollViewTheAnchor(TabLayout inside_tab, TabLayout outside_tab, ObservableScrollView scrollView, ArrayList<TextView> aimingPointList, Context context, float coverHeight) {
        this.mInside_tab = inside_tab;
        this.mOutside_tab = outside_tab;
        this.mScrollView = scrollView;
        this.mAimingPointList = aimingPointList;
        this.mContext = context;
        this.mCoverHeight = dip2px(mContext, coverHeight);
        mOutside_tab.setOnTabSelectedListener(this);
        mInside_tab.setOnTabSelectedListener(this);
        mScrollView.setScrollViewListener(this);
        mScrollView.setOnTouchListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        mOutside_tab.getTabAt(position).select();
        mInside_tab.getTabAt(position).select();
        mOutside_tab.setVisibility(View.VISIBLE);
        if (state == MotionEvent.ACTION_UP) {
            mScrollView.smoothScrollTo(0, mAimingPointList.get(position).getTop() - dip2px(mContext, 50));
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        state = event.getAction();
        return false;
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        /**
         * 已经滑动的距离
         */
        int scrolly = mScrollView.getScrollY();
        /**
         * 导航控制
         */
        if (scrolly >= mInside_tab.getTop()) {
            mOutside_tab.setVisibility(View.VISIBLE);
        } else {
            mOutside_tab.setVisibility(View.GONE);
        }
        /**
         * 遍历锚点 从最下面锚点开始遍历 如果已经滑动的y距离+tablayout高度大于锚点的top 选中该tab
         */
        for (int i = 0; i < mAimingPointList.size(); i++) {
            int index = mAimingPointList.size() - i - 1;
            int top = mAimingPointList.get(index).getTop();

            if (scrolly + mCoverHeight >= top) {
                mInside_tab.getTabAt(index).select();
                mOutside_tab.getTabAt(index).select();
                return;
            }
        }
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}
